#ifndef TCPDATARECEIVER_H
#define TCPDATARECEIVER_H

#include <QObject>
#include<QTcpSocket>
#include<QVariant>
#include <chrono>
#define CHRONO_START_TIMER() auto _chrono_start_time = std::chrono::high_resolution_clock::now()
#define CHRONO_ELAPSED_MS() std::chrono::duration_cast<std::chrono::milliseconds>(\
    std::chrono::high_resolution_clock::now() - _chrono_start_time).count()

class TcpDataReceiver : public QObject {
    Q_OBJECT
public:
    enum ReadMode {
        ReadUntilDelimiter,
        ReadFixedLength,
        ReadLengthPrefixed
    };

    explicit TcpDataReceiver(QTcpSocket* socket, QObject* parent);

    void setReadMode(ReadMode mode, QVariant parameter) ;

    void setTimeout(int timeoutMs) ;

    QByteArray readData() ;
    QByteArray readUntilDelimiter(QTcpSocket* socket, char delimiter, int timeoutMs);
    QByteArray readFixedLength(QTcpSocket* socket, int length, int timeoutMs);
    QByteArray readLengthPrefixedData(QTcpSocket* socket, int timeoutMs);

private:
    QTcpSocket* m_socket;
    ReadMode m_mode;
    QVariant m_parameter;
    int m_timeout;

    // 上面实现的读取方法可以放在这里作为私有方法

};


#endif // TCPDATARECEIVER_H
